<template>
	<div
		class="popoer"
		:style="{
			zIndex: isShowMenu ? 9999 : 1
		}"
	>
		<view class="mask" v-if="isShowMenu" @click.stop="showMenu"></view>
		<div class="reference" @click.stop="showMenu">
			<slot name="reference"></slot>
		</div>
		<div class="menu-body" :class="[placement]" :style="{ width: width + 'rpx' }">
			<div class="menu" :class="[theme, placement]" v-if="isShowMenu">
				<div @click="closeAllPopover">
					<slot></slot>
				</div>
				<div v-if="contents.length" style="padding: 10rpx 0;">
					<template v-for="(a, index) in contents">
						<view v-if="a?.showBtn" class="menu-item" :key="index" :class="{ disabled: a?.disabled }" @click="onSelect(a)">
							<view style="font-size: 32rpx">{{ a?.text }}</view>
						</view>
					</template>
				</div>
			</div>
		</div>
		<!-- <div v-if="isShowMenu" @click="closeAllPopover" class="mask"></div> -->
	</div>
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue';

const instance = getCurrentInstance();
const emit = defineEmits(['select']);

// 定义props
const props = defineProps({
	theme: {
		type: String,
		default: ''
	},
	contents: {
		type: Array,
		default: []
	},
	placement: {
		type: String,
		default: 'bottom'
	},
	width: {
		type: String,
		default: 'auto'
	}
});

// 定义响应式数据
const isShowMenu = ref(false);

// 定义方法
const closeAllPopover = () => {
	// 获取父组件的所有子组件（假设在Vue 3中组件实例获取方式类似，实际可能需要根据具体情况调整）
	const parentChildren = instance.parent;
	console.log(parentChildren, 'parentChildren');
	if (parentChildren && instance) {
		const parentChildren = instance.parent.proxy.$children;
		// parentChildren.forEach((item) => {
		// 	item.isShowMenu = false;
		// });
	}
};

const onSelect = item => {
	if (!item.disabled) {
		//   closeAllPopover();
		// 触发自定义事件
		emit('select', item);
		isShowMenu.value = false;
	}
};

const showMenu = e => {
	// if (!isShowMenu.value) closeAllPopover();
	isShowMenu.value = !isShowMenu.value;
};
</script>

<style lang="scss" scoped>
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0);
}
.popoer {
  position: relative;
  display: inline-block;
}
.popoer > div {
  z-index: 999;
}
.popoer .menu-body {
  position: absolute;
  z-index: 10;
}

.popoer .menu-body.bottom {
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}

.popoer .menu-body.bottom-start {
  bottom: 0;
  left: 0;
}

.popoer .menu-body.bottom-end {
  right: 0;
  bottom: 0;
}

.popoer .menu-body.top,
.popoer .menu-body.top-start,
.popoer .menu-body.top-end {
  top: 0;
}

.popoer .menu-body.top {
  left: 50%;
  transform: translateX(-50%);
}

.popoer .menu-body.top-end {
  right: 0;
}

.popoer .menu {
  position: absolute;
  right: 16rpx;
  padding: 10rpx 0;
  color: #515a6e;
  background: #ccc;
  background: #fff;
  box-shadow: 0upx 0upx 30upx rgba(0, 0, 0, .2);
  border-radius: 5px;
}

.popoer .menu::after {
  content: "";
  position: absolute;
  border-color: transparent transparent #fff;
  border-style: solid;
  border-width: 0 20rpx 20rpx;
}

.popoer .menu.dark {
  color: #fff;
  background: rgba(0, 0, 0, .81);
}

.popoer .menu.dark::after {
  border-color: transparent transparent rgba(0, 0, 0, .81);
}

.popoer .menu.light {
  color: #626262;
  background: rgba(255, 255, 255, 1);
}

.popoer .menu.light::after {
  border-color: transparent transparent rgba(255, 255, 255, 1);
}

.popoer .menu.bottom {
  top: 22rpx;
  right: 0;
  left: 0;
  margin: auto;
}

.popoer .menu.bottom-start {
  top: 22rpx;
  left: 0;
}

.popoer .menu.bottom-end {
  top: 22rpx;
  right: 0;
}

.popoer .menu.bottom::after {
  top: -18rpx;
  right: 10rpx;
  right: 50%;
  transform: translateX(50%);
}

.popoer .menu.bottom-start::after {
  top: -18rpx;
  right: 10rpx;
  right: initial;
  left: 10px;
}

.popoer .menu.bottom-end::after {
  top: -18rpx;
  right: 10rpx;
  right: 10px;
  left: initial;
}

.popoer .menu.top,
.popoer .menu.top-start,
.popoer .menu.top-end {
  bottom: 22rpx;
}

.popoer .menu.top {
  right: 0;
  left: 0;
  margin: auto;
}

.popoer .menu.top-start {
  left: 0;
}

.popoer .menu.top-end {
  right: 0;
}

.popoer .menu.top::after,
.popoer .menu.top-start::after,
.popoer .menu.top-end::after {
  bottom: -20rpx;
  border-color: rgba(0, 0, 0, .81) transparent transparent;
  border-style: solid;
  border-width: 20rpx 20rpx 0;
}

.popoer .menu.top::after {
  right: 50%;
  transform: translateX(50%);
}

.popoer .menu.top-start::after {
  left: 0;
}

.popoer .menu.top-end::after {
  right: 0;
}

.popoer .menu-item {
  box-sizing: border-box;
  padding: 10rpx;
  text-align: center;
  white-space: nowrap;
}

.popoer .menu-item.disabled {
  color: #c8c9cc;
  cursor: not-allowed;
}

.popoer .menu.dark .menu-item.disabled,
.popoer .menu.light .menu-item.disabled {
  color: #9fa0a2;
}

.popoer > div.mask {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
}
</style>
